package com.xckj.cycling.view

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.widget.FrameLayout
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebChromeClient
import com.just.agentweb.WebViewClient
import com.xckj.cycling.R
import com.xckj.cycling.framework.BaseActivityKt
import com.xckj.cycling.utils.DensityUtil
import com.xckj.cycling.utils.JsAction

class CWebView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {
    private val webViewClient: WebViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
            return super.shouldOverrideUrlLoading(view, request)
            //  view.loadUrl(url);
            // return true;
        }

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
        }

        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)

        }
    }

    private val mWebChromeClient: WebChromeClient = object : WebChromeClient() {
        override fun onReceivedTitle(view: WebView, title: String) {
            super.onReceivedTitle(view, title)
        }

        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            if (newProgress <= 20) {
                progress.visibility = View.VISIBLE
            } else if (newProgress >= 100) {
                progress.visibility = View.GONE
            }
        }
    }
    val progress = ImageView(context)
    val mAgentWeb: AgentWeb
    private val size = DensityUtil.dip2px(60f)

    init {

        mAgentWeb = AgentWeb.with(context as Activity)
            .setAgentWebParent(
                this,
                LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
            ).useDefaultIndicator()
            .interceptUnkownUrl()
            .closeWebViewClientHelper()
            .setWebViewClient(webViewClient)
            .setWebChromeClient(mWebChromeClient)
            .createAgentWeb()
            .ready().get()
        progress.apply {
            addView(this, LayoutParams(size, size, Gravity.CENTER))
            Glide.with(context).load(R.mipmap.loading).into(this)
        }
        bringChildToFront(progress)
        mAgentWeb.getAgentWebSettings().getWebSettings().setUseWideViewPort(true) //设置此属性，可任意比例缩放

        mAgentWeb.getAgentWebSettings().getWebSettings()
            .setLoadWithOverviewMode(true) //设置充满全屏幕,电脑网页适配手机屏幕

        mAgentWeb.getAgentWebSettings().getWebSettings().setJavaScriptEnabled(true)
        mAgentWeb.getAgentWebSettings().getWebSettings().setBuiltInZoomControls(true)
        mAgentWeb.getAgentWebSettings().getWebSettings().setAllowFileAccessFromFileURLs(true)
        mAgentWeb.getAgentWebSettings().getWebSettings().setAllowUniversalAccessFromFileURLs(true)
        WebView.setWebContentsDebuggingEnabled(true)
        mAgentWeb.getAgentWebSettings().getWebSettings().setDomStorageEnabled(true)
        //隐藏缩放控件
        //隐藏缩放控件
        mAgentWeb.getAgentWebSettings().getWebSettings().setDisplayZoomControls(false)


        mAgentWeb.getJsInterfaceHolder()
            .addJavaObject("phone", JsAction(context as BaseActivityKt, this))
    }

    fun loadUrl(url: String) {
        mAgentWeb.urlLoader.loadUrl(url)
    }

    fun goBack(): Boolean {
        return mAgentWeb.back()
    }
}